$(document).ready(function() {
    $("#form_type").change(function() { //dectect change of select
        var type = $(this).val();
        //alert(id+type);
        //gửi lên hàm TypeSetParents lay gia tri parents
        //alert(type);
        $.ajax({
            type: 'post',
            url: 'http://localhost/fuelphp-ml/public/transactions/typesetparents', //xem lại cách hiển thị link
            data: {"type": type},
            dataType: 'json',
            timeout: 10000,
            error: function(xhr, status, error) {
                alert('Error: ' + xhr.status + ' - ' + error);
            },
            success: function(response) {
               // console.log(response);// chay cai nay thi lai hoat dong
                $("#form_category_id").html('');
                for (var key in response) {
                    $("#form_category_id").append('<option value=' + key + '>' + response[key] + '</option>');


                }



            }



        });

    });
    /* $("#time12").click(function(){ 
     
     $("#time123").toggle(400);
     
     });*/
    $("#time12").toggle(
            function() {
                $("#TransactionTimeMonth").prop("selectedIndex", 1);
                $("#TransactionTimeDay").prop("selectedIndex", 1);
                $("#TransactionTimeYear").prop("selectedIndex", 1);
                $("#time123").show();
            }, function() {
        $("#TransactionTimeMonth").prop("selectedIndex", null);
        $("#time123").hide();
    }
    );

    $("#category12").toggle(
            function() {
                $("#form_type").prop("selectedIndex", 3);//set la null
                //$("label[for='form_type']").hide();
                //$("label[for='form_category_id']").hide();
                $("#form_category_id").hide();
                $("#form_type").hide();
            }, function() {
        $("#form_type").prop("selectedIndex", 1);
        $("#form_category_id").show();
        $("#form_type").show();
        //$("label[for='form_type']").show();
        //$("label[for='form_category_id']").show();
    }
    );
// xu li da tra
    $("#smdatra").click(function() { //dectect change of select
        var pr_id = $("#pr_id").text();
        var money = $("#TransactionMoney").val();

        $.ajax({
            type: 'post',
            url: '../../Transactions/addchild', //xem lại cách hiển thị link
            data: {"pr_id": pr_id, "money": money},
            dataType: 'json',
            timeout: 10000,
            error: function(xhr, status, error) {
                alert('Error: ' + xhr.status + ' - ' + error);
            },
            success: function(response) {
                if (response == 'invalid message') {
                    alert('invalid money');
                }
                else {
                    $("#child_tr").html(response);
                    var a = $("#pr_money").text();
                    if ((a - response) != 0) {
                        $("#child_tr1").html(a - response);
                    }
                    else {
                        $("#child_tr1").html('Đã trả hết');
                    }
                    $("#datra").hide(800);

                }
            }
        });



    });

    $("#edit").click(function() {
        $("#datra").toggle(400);

    });






});